local skynet = require "skynet"
local int64 = require "int64"


local function test_basic_operations()
  -- 测试基本运算
  local a = int64.new(100)
  local b = int64.new(50)

  assert(tostring(a + b) == "int64: 0x96", "Addition failed")
  assert(tostring(a - b) == "int64: 0x32", "Subtraction failed")
  assert(tostring(a * b) == "int64: 0x1388", "Multiplication failed")
  assert(tostring(a / b) == "int64: 0x2", "Division failed")
  assert(tostring(a % b) == "int64: 0x0", "Modulo failed")
  -- assert(tostring(-a) == "int64: -0x64", "Negation failed")  not pass

  skynet.error("Basic operations test passed")
end

local function test_comparisons()
  -- 测试比较运算
  local a = int64.new(100)
  local b = int64.new(200)

  assert(a < b, "Less than failed")
  assert(a <= b, "Less than or equal failed")
  assert(a == int64.new(100), "Equality failed")
  assert(a ~= b, "Inequality failed")

  skynet.error("Comparison tests passed")
end
local function test_int64_pack_unsigned()
  -- 使用字符串来表示大数字
  local test_value = 12345678901234567890 -- Lua 默认的数字类型已经足够表示该值
  local packed = int64.pack_u64(test_value)

  -- 确保打包数据长度为 8 字节
  assert(#packed == 8, "Expected packed data to be 8 bytes")

  -- 确保打包的字节数据与预期匹配
  assert(packed == "\x15\xCD\x5B\xF7\xC1\x3B\xCB\xD2", "Packed data does not match expected")
  print("int64_pack_unsigned test passed")
end

local function test_int64_unpack_unsigned()
  local test_value = 12345678901234567890 -- Lua 默认的数字类型已经足够表示该值
  local buffer = "\x15\xCD\x5B\xF7\xC1\x3B\xCB\xD2"
  local unpacked = int64.pack_u64(buffer)
  assert(unpacked == test_value, "Unpacked value does not match expected")
  print("pack_u64 test passed")

  local zero_buffer = "\x00\x00\x00\x00\x00\x00\x00\x00"
  unpacked = int64.int64_unpack_unsigned(zero_buffer)
  assert(unpacked == 0, "Unpacked value for zero does not match expected")
  print("pack_u64 boundary test passed")
end

local function test_int64_unpack_signed()
  local test_value = -1234567890123456789 -- Lua 默认的数字类型已经足够表示该值
  local buffer = "\x15\xCD\x5B\xF7\xC1\x3B\xCB\xD2"
  local unpacked = int64.unpack_i64(buffer)
  assert(unpacked == test_value, "unpack_i64 does not match")
end


local function test_unpack_f()
  local test_value = 3.141592
  local packed = "\xDB\x0F\x49\x40" -- 这个是 float 3.141592 的小端序字节序列
  local unpacked = int64.unpack_f(packed)
  assert(math.abs(unpacked - test_value) < 1e-6, "unpack_f failed: " .. unpacked)
  print("unpack_f test passed")

  -- 边界测试：0.0
  local zero = "\x00\x00\x00\x00"
  assert(int64.unpack_f(zero) == 0.0, "unpack_f zero test failed")
  print("unpack_f zero test passed")
end

-- 测试 pack_d（double 打包）
local function test_pack_d()
  local test_value = 3.141592653589793
  local packed = int64.pack_d(test_value)
  assert(#packed == 8, "pack_d returned wrong size")
  assert(packed == "\x18\xDD\x3F\xF3\xC0\xC8\xF6\x40", "pack_d wrong bytes")
  print("pack_d test passed")

  -- 边界测试：0.0
  local packed_zero = int64.pack_d(0.0)
  assert(packed_zero == "\x00\x00\x00\x00\x00\x00\x00\x00", "pack_d zero test failed")
  print("pack_d zero test passed")
end

-- 测试 unpack_d（double 解包）
local function test_unpack_d()
  local test_value = 3.141592653589793
  local packed = "\x18\xDD\x3F\xF3\xC0\xC8\xF6\x40" -- double 3.141592653589793
  local unpacked = int64.unpack_d(packed)
  assert(math.abs(unpacked - test_value) < 1e-15, "unpack_d failed: " .. unpacked)
  print("unpack_d test passed")

  -- 测试边界值（0.0）
  local zero = "\x00\x00\x00\x00\x00\x00\x00\x00"
  assert(int64.unpack_d(zero) == 0.0, "unpack_d zero test failed")
  print("unpack_d zero test passed")
end

skynet.start(function()
  skynet.error("Starting int64 tests...")
  test_basic_operations()
  test_comparisons()
  test_basic_operations()
  -- test_int64_pack_unsigned()
  -- test_int64_unpack_unsigned()
  -- test_int64_unpack_signed()
  --test_unpack_f()
  --test_pack_d()
  test_unpack_d()

  skynet.error("All int64 tests passed successfully!")
  skynet.exit()
end)
